package com.dst.smart.app;

import com.dst.smart.R;
import com.dst.smart.constant.Constants;
import com.dst.smart.dao.impl.IWidgetDAO;
import com.dst.smart.dao.impl.WidgetDAOProxy;
import com.iflytek.aiui.utils.log.DebugLog;
import com.iflytek.aiuilauncher.client.DeamonClient;
import com.dst.smart.utils.CrashCollector;
import android.app.Application;
import android.os.AsyncTask;
import android.os.Environment;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import android_serialport_api.SerialPort;
import android_serialport_api.SerialReceiveListener;

/**
 * Demo的Application对象。
 * 
 * author <a href="http://www.xfyun.cn">讯飞开放平台</a>
 * date 2016年6月28日 下午10:07:00
 *
 */
public class MyApplication extends Application implements SerialReceiveListener {
	private static final String TAG = Constants.TAG;
	public int isDown=0;
	private DeamonClient mDeamonClient;
	private static MyApplication instance = null;
	protected SerialPort mSerialPort;
	@Override
	public void onCreate() {
		CrashCollector collector = CrashCollector.getInstance();
		collector.init(getApplicationContext());
		instance = this;
		initFiles();
		InitSerial();
		// 注册守护
		//registerDeamon();
		DebugLog.LogD(TAG, "onCreate");
	}


	public static MyApplication getInstance() {
		return instance;
	}
	public void initFiles() {
		// TODO Auto-generated method stub
		isExite();
		String file = android.os.Environment.getExternalStorageDirectory()
				.getAbsolutePath()
				+ File.separator
				+ "ZunderHome"
				+ File.separator + "homedatabases.db";
		if (!new File(file).exists()) {
			FileOutputStream fos = null;
			InputStream is = null;
			try {
				is = this.getResources().openRawResource(R.raw.homedatabases);
				fos = new FileOutputStream(file);
				byte[] buffer = new byte[8192];
				int count = 0;
				while ((count = is.read(buffer)) > 0) {
					fos.write(buffer, 0, count);
					fos.flush();
				}
			} catch (Exception e) {
				System.out.println(e.toString());
			}
		}
	}

	public void isExite() {
		File sd = Environment.getExternalStorageDirectory();
		String path = sd.getPath() + "/ZunderHome";
		File file = new File(path);
		if (!file.exists()) {
			file.mkdir();
		}
	}
	public String getRootPath() {
		return android.os.Environment.getExternalStorageDirectory()
				.getAbsolutePath() + File.separator + "ZunderHome";
	}
	public String getMusicDirectory(String musicName) {

		String path= android.os.Environment.getExternalStorageDirectory()
				.getAbsolutePath() + File.separator + "ZunderHome"+File.separator+"music";
		File file = new File(path);
		if (!file.exists()) {
			file.mkdir();
		}
		String filePath= path+File.separator+musicName;
		if(new File(filePath).exists()){
			path+=File.separator+musicName;
		}else{
			path="";
		}
		return path;
	}
	public IWidgetDAO getWidgetDataBase() {
		return WidgetDAOProxy.getInstance();
	}
	public void InitSerial() {
		if (mSerialPort != null) {
			mSerialPort.close();
			mSerialPort = null;
		}
		try {
			new InitSerialTask().execute();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public SerialPort getSerialPort() throws Exception {
		if (mSerialPort == null) {

			String path = "/dev/ttyS2";
			int baudrate = 38400;

			if ((path.length() == 0) || (baudrate == -1)) {
				throw new Exception();
			}
			mSerialPort = new SerialPort(new File(path), baudrate, 0);
		}
		return mSerialPort;
	}
	public void setSerial(SerialPort serialPort) {
		if (serialPort != null) {
			this.mSerialPort = serialPort;
			mSerialPort.setListener(this);
		}

	}
	private class InitSerialTask extends AsyncTask<String, Integer, SerialPort> {

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
		}

		@Override
		protected void onPostExecute(SerialPort s) {
			// TODO Auto-generated method stub

			setSerial(s);
			try {
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			super.onPostExecute(s);
		}

		@Override
		protected SerialPort doInBackground(String... params) {
			// TODO Auto-generated method stub
			SerialPort newSerialPort = null;
			try {
				newSerialPort = getSerialPort();
			} catch (Exception e) {
				// TODO: handle exception
				System.out.println(this.getClass() + ":" + e.toString());
			}
			return newSerialPort;
		}
	}

	public void closeSerialPort() {

		if (mSerialPort != null) {
			mSerialPort.close();
			mSerialPort = null;
		}
	}
	public void SendDate(byte[] msg) throws Exception {
		try {
			mSerialPort.SendData(msg);

		} catch (Exception e) {
		}
	}

	@Override
	public void serialReceive(String s) {

		MainActivity.activity.serialReceive(s);
	}
}
